<?php

namespace app\common\model;

//use think\Model;

class UserBalanceModel extends BaseModel{

	protected $table = 'tp_user_balance';

	/*
	更新账户
	 */
	public function updateBalance($uid,$wid,$balance){
		$user_balance = $this->where('uid',$uid)->where('wid',$wid)->find();
	    if(empty($user_balance)){
	        $res = $this->insert([
	          	'uid'=>$uid,
	          	'wid'=>$wid,
	          	'balance'=>$balance,
	          	'date'=>$this->getDate()
	        ]);
	    } else {
	    	$up_balance = $user_balance['balance'] + $balance;
	    	if ($up_balance < 0) {
	    		$res = false;
	    	} else {
				$res = $this->where('id',$user_balance['id'])->update([
					'balance'=>$up_balance,
				]);
	    	}
	    }
	    return $res;
	}

	/*
	获取账户金额
	*/
	public function getBalance($uid,$wid){
		$data = $this->where('uid',$uid)->where('wid',$wid)->value('balance')?:0;
		$data = $data/1;
	    return $data;
	}

	/*
	获取账户金额
	*/
	public function getBalance2($uid,$wid){
		$data = $this->where('uid',$uid)->where('wid',$wid)->find();
		$data['balance'] = $data['balance']/1;
		$data['reg_balance'] = $data['reg_balance']/1;
		$data['agent_balance'] = $data['agent_balance']/1;
	    return $data;
	}
}